﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Zor_Modes;
using Zor_Tools;
using Zor_Bll;
using Newtonsoft.Json.Linq;

namespace Zor_API.Controllers
{
    public class FlowController : ApiController
    {
        /// <summary>
        /// 获取办事流程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<M_Information> FlowList()
        {
            List<M_Information> lst = Flow_Bll.GetFlowList();
            return lst;
        }

        /// <summary>
        /// 新增办事流程
        /// </summary>
        /// <param name="infomation"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage InsertFlow([FromBody]M_Information infomation)
        {
            BaseAnswer bar = Flow_Bll.AddFlow(infomation);

            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }

        /// <summary>
        /// 获取办事流程详情
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public M_Information InfoMationById([FromBody]JObject obj)
        {
            if (obj == null) return new M_Information();
            var json = obj["id"].ToString();
            M_Information infomation = Flow_Bll.GetTById(Convert.ToInt32(json));
            return infomation;
        }

        /// <summary>
        /// 编辑办事流程
        /// </summary>
        /// <param name="infomation"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage EditFlow([FromBody]M_Information infomation)
        {
            BaseAnswer bar = Flow_Bll.EditFlow(infomation);

            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }

        /// <summary>
        /// 删除办事流程
        /// </summary>
        /// <param name="infomation">id集合</param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage DelFlow([FromBody]JObject infomation)
        {
            dynamic json = infomation;

            JArray jnotice = json.infomation;
            List<int> lst = new List<int>();
            foreach (var item in jnotice)
            {
                lst.Add(item.ToObject<M_Information>().Info_Id);
            }
            BaseAnswer bar = Flow_Bll.DeleteFlow(lst.ToArray());
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;

        }

    }
}
